package com.oe.market.modules.ele.validator;



import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EleRedPackageValidator implements ConstraintValidator<EleRedPackage, String> {

    // https://h5.ele.me/hongbao/#hardware_id=&is_lucky_group=True&lucky_number=0&track_id=&platform=0&sn=2a0ebed8edadcc71&theme_id=3201&device_id=&refer_user_id=275780192

    private String eleUrlReg = "^https://h5.ele.me/hongbao/";
    public Pattern eleUrlPattern = Pattern.compile(eleUrlReg);
    private String paramsReg = "(lucky_number=\\d([&\\w=]+)sn=[a-z0-9]+)";
    public Pattern paramsPattern = Pattern.compile(paramsReg);

    public void initialize(EleRedPackage money) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (s == null)
            return true;

        Matcher ele = eleUrlPattern.matcher(s);
        if (ele.find()) {
            Matcher params = paramsPattern.matcher(s);
            if (params.find())
                return true;
        }
        return false;
    }
    public static void main(String args[]){
//        String content = "https://h5.ele.me/hongbao/#hardware_id=&is_lucky_group=True&lucky_number=0&track_id=&platform=0&sn=2a0ebed8edadcc71&theme_id=3201&device_id=&refer_user_id=275780192";
//
//        String pattern = "lucky_number=";
//        EleRedPackageValidator eleRedPackageValidator = new EleRedPackageValidator();
//        Matcher ele = eleRedPackageValidator.eleUrlPattern.matcher(content);
//        if (ele.find()) {
//            Matcher params = eleRedPackageValidator.paramsPattern.matcher(content);
//            if (params.find())
//                System.out.println("字符串中是否包含了 'runoob' 子字符串?  true");
//        }
//
//        System.out.println("字符串中是否包含了 'runoob' 子字符串? ");
    }


}
